package mrHunter.data;

import mrHunter.UserInf;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

@Repository
public class JdbcUserRepository implements UserRepository {

    private JdbcTemplate jdbcTemplate;

    @Autowired
    public JdbcUserRepository(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public boolean save(UserInf user) {
        int isName = jdbcTemplate.queryForObject("select count(*) from userinf where username=? ",Integer.TYPE,user.getUsername());
        int isEmail=jdbcTemplate.queryForObject("select count(*) from userinf where email=? ",Integer.TYPE,user.getEmail());

        if(0==isEmail&&0==isName){
            insertUser(user);
            return true;
        }
        return false;
    }

    @Override
    public void updateCurrentLevel(String username, String currentLevel){
        System.out.println(username);
        System.out.println(currentLevel);
        jdbcTemplate.update("update userinf set currentLevel=? where username=?",new Object[] {currentLevel,username});
        return;
    }

    @Override
    public boolean passwordCheck(String username, String password) {
        int isName=jdbcTemplate.queryForObject("select count(*) from userinf where username=? ",Integer.TYPE,username);
        if(0==isName)
            return false;
        else
            return password.equals(jdbcTemplate.queryForObject("select password from userinf where username=?", String.class, username));
    }

    private void insertUser(UserInf user) {
        SimpleJdbcInsert jdbcInsert = new SimpleJdbcInsert(jdbcTemplate).withTableName("userinf");
        //jdbcInsert.setGeneratedKeyName("id");
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("username", user.getUsername());
        args.put("password", user.getPassword());
        args.put("email", user.getEmail());
        args.put("passTutorial", 0);
        String currentLevel="";
        for(int i=0;i<user.getCurrentLevel().length;i++){
            currentLevel+=user.getCurrentLevel()[i];
            if(i!=user.getCurrentLevel().length-1)
                currentLevel+=",";
        }
        args.put("currentLevel",currentLevel);
        jdbcInsert.execute(args);
        return;
    }

    @Override
    public UserInf findByUserName(String name) {
        return jdbcTemplate.queryForObject("select username, password, email, passTutorial,currentLevel from userinf where username=?", new UserRowMapper(), name);
    }

    private static class UserRowMapper implements RowMapper<UserInf> {
        public UserInf mapRow(ResultSet rs, int rowNum) throws SQLException {
            return new UserInf(
                    rs.getString("username"),
                    rs.getString("password"),
                    rs.getString("email"),
                    rs.getInt("passTutorial"),
                    rs.getString("currentLevel"));
        }
    }

}
